package com.firefly.admin.monitor.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.firefly.admin.common.vo.Result;
import com.firefly.admin.monitor.entity.FyMonitorLoginLog;
import com.firefly.admin.monitor.mapper.FyMonitorLoginLogMapper;
import com.firefly.admin.monitor.service.FyMonitorLoginLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import com.firefly.admin.monitor.vo.MonitorLoginLogQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class FyMonitorLoginLogServiceImpl implements FyMonitorLoginLogService {
    @Autowired
    private FyMonitorLoginLogMapper fyMonitorLoginLogMapper;

    /**
     * 新增登录日志
     *
     * @param fyMonitorLoginLog 登录日志信息
     */
    @Override
    public void save(FyMonitorLoginLog fyMonitorLoginLog) {
        fyMonitorLoginLogMapper.insert(fyMonitorLoginLog);
    }

    /**
     * 分页查询登录日志接口
     *
     * @param monitorLoginLogQueryVo 分页查询参数
     * @return 分页列表
     */
    @Override
    public Result<Page<FyMonitorLoginLog>> getMonitorLoginLogList(MonitorLoginLogQueryVo monitorLoginLogQueryVo) {
        log.info("###########StMonitorLoginLogServiceImpl getMonitorLoginLogList param = {}", JSONObject.toJSONString(monitorLoginLogQueryVo));
        LambdaQueryWrapper<FyMonitorLoginLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(monitorLoginLogQueryVo.getOperateResult()), FyMonitorLoginLog::getOperateResult, monitorLoginLogQueryVo.getOperateResult());
        queryWrapper.eq(StringUtils.isNotBlank(monitorLoginLogQueryVo.getOperateType()), FyMonitorLoginLog::getOperateType, monitorLoginLogQueryVo.getOperateType());
        queryWrapper.orderByDesc(FyMonitorLoginLog::getLogId);
        Page<FyMonitorLoginLog> page = new Page<>(monitorLoginLogQueryVo.getPage(), monitorLoginLogQueryVo.getLimit());
        Page<FyMonitorLoginLog> monitorOperationLogPage = fyMonitorLoginLogMapper.selectPage(page, queryWrapper);
        return Result.success(monitorOperationLogPage);
    }
}
